﻿/*****************************************************************************
	SNEGA STG GAME ENGINE 0.7a
	Copyright (C) 2009, Xiaohan Tian (snega0223)
	
	http://code.google.com/p/sng-actionscript-stg-engine/
	
	This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License(LGPL) as
	published by the Free Software Foundation, either version 3 of the 
	License, or(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU Lesser General Public License for more details.
	
	You should have received a copy of the GNU Lesser General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*****************************************************************************/

package com.sng.stg.game.enemy 
{
	import com.sng.stg.game.enemy.action.EnemyAction;
	import com.sng.stg.game.enemy.action.EnemyConFireType1;
	import com.sng.stg.game.enemy.action.EnemyFireType1;
	import com.sng.stg.game.enemy.action.EnemyKill;
	import com.sng.stg.game.enemy.action.EnemyMove;
	import com.sng.stg.game.enemy.action.EnemyRemove;
	import com.sng.stg.game.event.ContinuousEvent;
	import com.sng.stg.game.event.EnemyKilled;
	import com.sng.stg.game.StageViewer;
	import flash.display.*;
	
	/**
	 * ...
	 * @author snega
	 */
	public class EnemyType1 extends Enemy
	{
		public var _time:int = 0;
		
		public function EnemyType1(currStage:StageViewer, node:XML, bitmapData:BitmapData = null, killedBitmapData:BitmapData = null, pixelSnapping:String = "auto", smoothing:Boolean = false) 
		{
			super(bitmapData, pixelSnapping, smoothing);
			
			this._currStage = currStage;
			
			this._killedAnimation = new EnemyKilled(killedBitmapData, pixelSnapping, smoothing);
			
			this._life = parseInt(node.life.toString());
			
			// start position
			this.x = parseInt(node.position.@x);
			this.y = parseInt(node.position.@y);
			this.z = parseInt(node.position.@z);
			
			// event list
			var actionNode:EnemyAction = null;
			var totalTime:int = 0;
			for each (var action:XML in node.actions.elements()) {
				trace("find action = " + action.@type);
				
				totalTime += parseInt(action.@time);
				
				if (action.@type == "change_move") {
					actionNode = new EnemyMove(false, totalTime, this, parseInt(action.@x), parseInt(action.@y), parseInt(action.@u), parseInt(action.@v));
					this._actionList.pushToEnd(actionNode);
				}
				else if (action.@type == "kill") {
					actionNode = new EnemyKill(false, totalTime, this);
					this._actionList.pushToEnd(actionNode);
				}
				else if (action.@type == "remove") {
					actionNode = new EnemyRemove(false, totalTime, this);
					this._actionList.pushToEnd(actionNode);
				}
				else if (action.@type == "fire_1") {
					actionNode = new EnemyFireType1(false, totalTime, this, parseInt(action.@d), parseInt(action.@ax), parseInt(action.@ay), parseFloat(action.@r), this._currStage._imageBitmapMap[action.@image]);
					this._actionList.pushToEnd(actionNode);
				}
				else if (action.@type == "continuous_fire_1") {
					actionNode = new EnemyConFireType1(false, totalTime, this, parseInt(action.@duration), parseInt(action.@angel), parseInt(action.@step), parseInt(action.@speed), parseInt(action.@r), this._currStage._imageBitmapMap[action.@image]);
					this._actionList.pushToEnd(actionNode);
				}
			}
		}
		
		override public function start(currStage:StageViewer):void {
			this._currStage = currStage;
			
			this._currStage._2dElement.addChild(this);
			this._currStage._currEnemyList.push(this);
			
			this._currAction = this._actionList._next;
			
			this._time = 0;
		}
		
		override public function process():void {
			// time and action
			this._time++;
			
			if (this._currAction != null) {
				if (this._currAction._monitorLife) {
					
				}
				else {
					if (this._currAction._time == this._time) {
						this._currAction.execute();
						this._currAction = this._currAction._next;
					}
				}
			}
			
			// position and rotation
			this.x += this._offsetPosX;
			this.y += this._offsetPosY;
			this.rotation += this._offsetRotX;
		}
		
	}
	
}